/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package quests.Q111_ElrokianHuntersProof;

import net.sf.l2j.gameserver.model.actor.L2Npc;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.quest.Quest;
import net.sf.l2j.gameserver.model.quest.QuestState;
import net.sf.l2j.gameserver.model.quest.State;

public class Q111_ElrokianHuntersProof extends Quest
{
	private final static String qn = "Q111_ElrokianHuntersProof";
	
	//NPCs
	private static final int MARQUEZ = 32113;
	private static final int MUSHIKA = 32114;
	private static final int ASAMAH = 32115;
	private static final int KIRIKASHIN = 32116;
	
	//Items
	private static final int FRAGMENT = 8768;
	private static final int ADENA = 57;
	
	public Q111_ElrokianHuntersProof(int questId, String name, String descr)
	{
		super(questId,name,descr);
		
		questItemIds = new int[] { FRAGMENT };
		
		addStartNpc(MARQUEZ);
		addTalkId(MARQUEZ, MUSHIKA, ASAMAH, KIRIKASHIN);
		
		addKillId(22196, 22197, 22198, 22218, 22200, 22201, 22202, 22219, 22208, 22209, 22210, 22221, 22203, 22204, 22205, 22220);
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		if (event.equalsIgnoreCase("32113-002.htm"))
		{
			st.set("cond", "1");
            st.playSound("ItemSound.quest_accept");
            st.setState(State.STARTED);
		}
		else if (event.equalsIgnoreCase("32115-002.htm"))
		{
			st.set("cond", "3");
			st.playSound("ItemSound.quest_middle");
		}
		else if (event.equalsIgnoreCase("32113-009.htm"))
		{
			st.set("cond", "4");
			st.playSound("ItemSound.quest_middle");
		}
		else if (event.equalsIgnoreCase("32113-018.htm"))
		{
			st.set("cond", "6");
			st.playSound("ItemSound.quest_middle");
			st.takeItems(FRAGMENT, -1);
		}
		else if (event.equalsIgnoreCase("32116-003.htm"))
		{
			st.playSound("EtcSound.elcroki_song_full");
			st.set("cond", "7");
		}
		else if (event.equalsIgnoreCase("32116-005.htm"))
		{
			st.set("cond", "8");
			st.playSound("ItemSound.quest_middle");
		}
		else if (event.equalsIgnoreCase("32115-004.htm"))
		{
			st.set("cond", "9");
			st.playSound("ItemSound.quest_middle");
		}
		else if(event.equalsIgnoreCase("32115-006.htm"))
		{
			st.set("cond", "10");
			st.playSound("ItemSound.quest_middle");
		}
		else if (event.equalsIgnoreCase("32116-007.htm"))
		{
			st.takeItems(8773, 1);
            st.giveItems(8763, 1);
            st.giveItems(8764, 100);
            st.rewardItems(ADENA, 1071691);
            st.addExpAndSp(553524, 55538);
            st.playSound("ItemSound.quest_finish");
            st.exitQuest(false);
		}
		
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		String htmltext = Quest.getNoQuestMsg(player);
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		switch (st.getState())
		{
			case State.COMPLETED:
				htmltext = Quest.getAlreadyCompletedMsg(player);
				break;
				
			case State.CREATED:
				if (st.getPlayer().getParty().isLeader(player))
				{
					if (st.getPlayer().getLevel() >= 75)
						htmltext = "32113-001.htm";
					else
					{
						st.exitQuest(true);
						htmltext = "32113-000a.htm";
					}
				}
				else
				{
					htmltext = "32113-000b.htm";
					st.exitQuest(true);
				}
				break;
				
			case State.STARTED:
				int cond = st.getInt("cond");
				switch (npc.getNpcId())
				{
					case MARQUEZ:
						if (cond == 1)
							htmltext = "32113-002.htm";
						else if (cond == 3)
							htmltext = "32113-003.htm";
						else if (cond == 5)
							htmltext = "32113-010.htm";
						break;
						
					case MUSHIKA:
						if (cond == 1)
						{
							htmltext = "32114-001.htm";
							st.set("cond", "2");
							st.playSound("ItemSound.quest_middle");
						}
						break;
						
					case ASAMAH:
						if (cond == 2)
							htmltext = "32115-001.htm";
						else if (cond == 3)
							htmltext = "32115-002.htm";
						else if (cond == 8)
							htmltext = "32115-003.htm";
						else if (cond == 9)
							htmltext = "32115-004.htm";
						else if (cond == 11)
						{
							htmltext = "32115-007.htm";
							st.set("cond", "12");
							st.playSound("ItemSound.quest_middle");
							st.giveItems(8773, 1);
							st.takeItems(8770, -1);
							st.takeItems(8771, -1);
							st.takeItems(8772, -1);
						}
						break;
						
					case KIRIKASHIN:
						if (cond < 6)
							htmltext = "32116-008.htm";
						else if (cond == 6)
							htmltext = "32116-001.htm";
						else if (cond == 7)
							htmltext = "32116-004.htm";
						else if (cond == 12)
							htmltext = "32116-006.htm";
						break;
				}
				break;
		}
		
		
		return htmltext;
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		QuestState st = player.getParty().getLeader().getQuestState(qn);
		if (st == null)
			return null;
		
		switch (npc.getNpcId())
		{
			case 22196:
			case 22197:
			case 22198:
			case 22218:
				if (st.getInt("cond") == 4)
				{
					if (st.getRandom(100) < 25)
						st.giveItems(FRAGMENT, 1);
					if (st.getQuestItemsCount(FRAGMENT) <= 49)
						st.playSound("ItemSound.quest_itemget");
					else
					{
						st.set("cond", "5");
						st.playSound("ItemSound.quest_middle");
					}
				}
				break;
				
			case 22200:
			case 22201:
			case 22202:
			case 22219:
				if (st.getInt("cond") == 10)
				{
					if (st.getRandom(100) < 75)
						st.giveItems(8770, 1);
					if (st.getQuestItemsCount(8770) <= 9)
						st.playSound("ItemSound.quest_itemget");
					if (st.getQuestItemsCount(8770) >= 10 && st.getQuestItemsCount(8771) >= 10 && st.getQuestItemsCount(8772) >= 10)
					{
						st.set("cond", "11");
						st.playSound("ItemSound.quest_middle");
					}
				}
				break;
				
			case 22208:
			case 22209:
			case 22210:
			case 22221:
				if (st.getInt("cond") == 10)
				{
					if (st.getRandom(100) < 75)
						st.giveItems(8772, 1);
					if (st.getQuestItemsCount(8772) <= 9)
						st.playSound("ItemSound.quest_itemget");
					if (st.getQuestItemsCount(8770) >= 10 && st.getQuestItemsCount(8771) >= 10 && st.getQuestItemsCount(8772) >= 10)
					{
						st.set("cond", "11");
						st.playSound("ItemSound.quest_middle");
					}
				}
				break;
				
			case 22203:
			case 22204:
			case 22205:
			case 22220:
				if (st.getInt("cond") == 10)
				{
					if (st.getRandom(100) < 75)
						st.giveItems(8771, 1);
					if (st.getQuestItemsCount(8771) <= 9)
						st.playSound("ItemSound.quest_itemget");
					if (st.getQuestItemsCount(8770) >= 10 && st.getQuestItemsCount(8771) >= 10 && st.getQuestItemsCount(8772) >= 10)
					{
						st.set("cond", "11");
						st.playSound("ItemSound.quest_middle");
					}
				}
				break;
		}
		
		return null;
	}
	
	public static void main(String[] args)
	{
		new Q111_ElrokianHuntersProof(111, qn, "Elrokian Hunters Proof");
	}
}